//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hakim.wfb.customer.util;

import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil {

    private static final Logger logger = LoggerFactory.getLogger(DigestUtil.class);

    public static final String DIGEST_SPLIT = "|";

    public DigestUtil() {
    }

    private static MessageDigest getDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException var2) {
            return null;
        }
    }

    private static MessageDigest getMd5Digest() {
        return getDigest("MD5");
    }

    private static MessageDigest getShaDigest() {
        return getDigest("SHA");
    }

    private static MessageDigest getSha256Digest() {
        return getDigest("SHA-256");
    }

    private static MessageDigest getSha384Digest() {
        return getDigest("SHA-384");
    }

    private static MessageDigest getSha512Digest() {
        return getDigest("SHA-512");
    }

    public static String MD5(byte[] data) {
        byte[] digestData = getMd5Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }

    public static String SHA(byte[] data) {
        byte[] digestData = getShaDigest().digest(data);
        return Hex.encodeHexString(digestData);
    }

    public static String SHA256(byte[] data) {
        byte[] digestData = getSha256Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }

    public static String SHA384(byte[] data) {
        byte[] digestData = getSha384Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }

    public static String SHA512(byte[] data) {
        byte[] digestData = getSha512Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }

    public static String getCombineDigest(String agreementDigest, String dateStr, String userName, String IDNumber) {
        String digestStr = "";
        Object b = null;

        try {
            try {
                byte[] b1 = (agreementDigest + "|" + dateStr + "|" + userName + "|" + IDNumber).getBytes("UTF-8");
                digestStr = SHA256(b1);
            } catch (UnsupportedEncodingException var7) {
                logger.error("", var7);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return digestStr;
    }

    public static String getAgreementDigest(String plainTxt) {
        String digestStr = "";
        Object b = null;

        try {
            byte[] b1 = plainTxt.getBytes("UTF-8");
            digestStr = SHA256(b1);
        } catch (UnsupportedEncodingException var4) {
            logger.error("", var4);
        }

        return digestStr;
    }

    public static String shiftEncode(String str, int[] key) {
        char[] chr = str.toCharArray();

        for(int i = 0; i < chr.length; ++i) {
            swap(chr, i, key[i % key.length] % chr.length);
        }

        return String.valueOf(chr);
    }

    public static String shiftEncode(String str, String key) {
        return shiftEncode(str, toIntArray(key));
    }

    public static String shiftDecode(String str, int[] key) {
        char[] chr = str.toCharArray();

        for(int i = chr.length - 1; i >= 0; --i) {
            swap(chr, i, key[i % key.length] % chr.length);
        }

        return String.valueOf(chr);
    }

    public static String shiftDecode(String str, String key) {
        return shiftDecode(str, toIntArray(key));
    }

    public static int[] toIntArray(String str) {
        int[] arr = new int[str.length()];

        for(int i = 0; i < str.length(); ++i) {
            arr[i] = str.charAt(i);
        }

        return arr;
    }

    public static void swap(char[] chr, int x, int y) {
        if(x < chr.length && y < chr.length && x != y) {
            char e = chr[x];
            chr[x] = chr[y];
            chr[y] = e;
        }

    }

    public static void main(String[] args) {
        String key = "鼎有财2016";
        String origin = "1234567890abcdefghijklmnopq";
        String encoded = shiftEncode(origin, key);
        String decoded = shiftDecode(encoded, key);
        System.out.println(origin);
        System.out.println(encoded);
        System.out.println(decoded);
    }
}
